-- UIUnrealEnter
-- Created by chengb Sep/21/2015
-- 夹层迷宫进入界面

UIUnrealEnter = class("UIUnrealEnter", function()
    return cc.Layer:create();
end);

-- @param pos       格子位置
-- @param unrealId  夹层编号
-- @param desc      夹层描述（如果不传，默认从配置表中读取）
-- @param isLeave   是否离开
function UIUnrealEnter.create(pos, unrealId, desc, isLeave)
    return UIUnrealEnter.new(pos, unrealId, desc, isLeave);
end

-- 构造函数
function UIUnrealEnter:ctor(pos, unrealId, desc, isLeave)
    self:setName("UIUnrealEnter");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.pos = pos;
    self.unrealId = unrealId;

    local unrealInfo = UnrealDungeonM.query(self.unrealId);
    if type(desc) == "string" then
        self.desc = desc;
    else
        self.desc = unrealInfo["desc"];
    end

    self.isLeave = isLeave or false;

    if self.isLeave then
        -- 离开
        self.okText = getLocStr("btn_text_leave");
    else
        if type(unrealInfo["ok_text"]) == "string" and unrealInfo["ok_text"] ~= "" then
            self.okText = unrealInfo["ok_text"];
        else
            -- 默认显示"进入"
            self.okText = getLocStr("btn_text_enter");
        end
    end

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIUnrealEnter:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIUnrealEnter:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIUnrealEnter 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIUnrealEnter:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIUnrealEnter");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 如果是离开，需要隐藏返回按钮
    btnBack:setVisible(iif(self.isLeave, false, true));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            if self.isLeave then
                -- 如果是离开，直接关闭当前界面
                UIMgr.getCurrentScene():removeFormByName("UIUnrealEnter");
            else
                -- 尝试进入
                self:tryStart();

                -- 关闭当前界面
                UIMgr.getCurrentScene():removeFormByName("UIUnrealEnter");
            end
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(self.okText);
end

-- 尝试进入夹层
function UIUnrealEnter:tryStart()
    local dbase = UnrealDungeonM.query(self.unrealId, "dbase");
    if dbase["is_arena"] == 1 then
        -- 如果是角斗台，判断能否进入角斗台
        if true ~= ArenaM.canEnterArena() then
            alert(getLocStr("arena_refuse_enter"));
            return false;
        end
    end

    -- 进入前提示
    local beforeEnterTip = UnrealDungeonM.query(self.unrealId, "before_enter_tip");
    if type(beforeEnterTip) == "string" and beforeEnterTip ~= "" then
        alert(beforeEnterTip);
    end

    return DungeonActionM.go("start_unreal", self.pos, self.unrealId);
end

-- 重绘
function UIUnrealEnter:redraw()
    local CT = findChildByName(self.node, "CT");
    local BT = findChildByName(self.node, "BT");

    -- 显示标题
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    local unrealInfo = UnrealDungeonM.query(self.unrealId);
    local titleLabel = findChildByName(CT, "title_node/title");
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning);
    titleLabel:setString(unrealInfo["name"])
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 显示建筑图标
    local iconImg = findChildByName(CT, "icon");
    iconImg:loadTexture(getLevelItemIconPath(unrealInfo["icon"]));
    iconImg:setScale(1.3);

    -- 显示功能描述
    local tipLabel = findChildByName(CT, "desc");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);
    applyString(tipLabel, self.desc);
end
